/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.fraclite.orb.io.messages;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Method;

/**
 * @author Sylvain Sicard
 */
public class MethodCall implements Externalizable {

  private static final long serialVersionUID = 7873471327078957662L;

  /** The name of the method, case sensitive. */
  private String        methodName;

  /** The arguments of the method. */
  private byte[]        args;

  /** The class types, e.g., new Class[]{String.class, int.class}. */
  private Class<?>[]    types;

  protected String        callId;

  public MethodCall() {
  }

  public MethodCall(String callId, Method method, byte[] arguments) {
    this.methodName = method.getName();
    this.types = method.getParameterTypes();
    this.args = arguments;
    this.callId = callId;
  }
//
//  /**
//   * returns an ordered list of arguments used for the method invokation
//   * 
//   * @return returns the list of ordered arguments
//   */
//  public byte[] getArgs() {
//    return args;
//  }
//
//  public void setArgs(byte[] args) {
//    if (args != null)
//      this.args = args;
//  }

  public String toString() {
    StringBuffer ret = new StringBuffer();
    ret.append(callId+" ");
    ret.append(getMethodName());
//    ret.append('(');
//    if (getArgs() != null) {
//      for (int i = 0; i < getArgs().length; i++) {
//        if (first)
//          first = false;
//        else ret.append(", ");
//        ret.append(getArgs()[i]);
//      }
//    }
//    ret.append(')');
    return ret.toString();
  }
//
//  public String toStringDetails() {
//    StringBuffer ret = new StringBuffer();
//    ret.append("MethodCall ");
//    ret.append("CallId=").append(callId).append("\n");
//    ret.append("name=").append(method_name);
//    ret.append(", number of args=").append((args != null ? args.length : 0)).append(')');
////    if (args != null) {
////      ret.append("\nArgs:");
////      for (int i = 0; i < args.length; i++) {
////        ret.append("\n[").append(args[i]).append(" (").append((args[i] != null ? args[i].getClass().getName() : "null")).append(")]");
////      }
////    }
//    return ret.toString();
//  }

  public void writeExternal(ObjectOutput out) throws IOException {
    out.writeUTF(getMethodName());
    out.writeUTF(callId);
    out.writeObject(getArgs());

    out.writeObject(getTypes());

//    if (payload != null) {
//      out.writeBoolean(true);
//      out.writeObject(payload);
//    } else {
//      out.writeBoolean(false);
//    }
  }

  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    methodName=in.readUTF();
    callId = in.readUTF();
    args = (byte[]) in.readObject();
    types = (Class[]) in.readObject();
//    boolean payload_available = in.readBoolean();
//    if (payload_available) {
//      payload = (Map) in.readObject();
//    }
  }

  public String getCallId() {
    return callId;
  }
//
//  public void setMethodName(String method_name) {
//    this.methodName = method_name;
//  }

  public String getMethodName() {
    return methodName;
  }
//
//  public void setTypes(Class<?>[] types) {
//    this.types = types;
//  }

  public Class<?>[] getTypes() {
    return types;
  }
//
//  public void setArgs(byte[] args) {
//    this.args = args;
//  }

  public byte[] getArgs() {
    return args;
  }

}
